package com.briup.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.cms.bean.Slideshow;
import com.briup.cms.dao.SlideshowDao;
import com.briup.cms.exception.ServiceException;
import com.briup.cms.service.ISlideshowService;
import com.briup.cms.util.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class SlideshowServiceImpl implements ISlideshowService {
    @Autowired
    private SlideshowDao slideshowDao;

    @Override
    public Slideshow queryById(Integer id) {
        Slideshow slideshow = slideshowDao.selectById(id);
        if (slideshow == null){
            throw new ServiceException(ResultCode.DATA_NONE);
        }
        return slideshow;
    }

    @Override
    public List<Slideshow> queryAllEnable() {
        //lambda格式按条件查询
        LambdaQueryWrapper<Slideshow> lqw = new LambdaQueryWrapper<Slideshow>();
        lqw.eq(Slideshow::getStatus, "启用").orderByDesc(Slideshow::getUploadTime);
        List<Slideshow> userList = slideshowDao.selectList(lqw);
        if(userList == null || userList.size() == 0) {
            throw new ServiceException(ResultCode.DATA_NONE);
        }
        return userList;
    }

    @Override
    public IPage<Slideshow> query(Integer pageNum, Integer pageSize, String status, String description) {
        //1 创建IPage分页对象,设置分页参数
        IPage<Slideshow> page = new Page<>(pageNum, pageSize);
        //2.执行分页+条件查询
        LambdaQueryWrapper<Slideshow> lqw = new LambdaQueryWrapper<Slideshow>();
        lqw.eq(status != null, Slideshow::getStatus,status)
                .like(description != null,Slideshow::getDescription,description)
                .orderByDesc(Slideshow::getUploadTime);
        IPage<Slideshow> slideshowIPage = slideshowDao.selectPage(page, lqw);
        if (slideshowIPage.getTotal() == 0){
            throw new ServiceException(ResultCode.DATA_NONE);
        }
        //3 获取分页结果
        return slideshowIPage;
    }

    @Override
    public void deleteInBatch(List<Integer> ids) {
        //如果集合为null或为空，抛出参数为空异常
        if (ids == null || ids.isEmpty()){
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        }
        //如果集合不为空，根据ids查找轮播图
        LambdaQueryWrapper<Slideshow> lqw = new LambdaQueryWrapper<>();
        lqw.in(Slideshow ::getId,ids);
        int num = slideshowDao.selectCount(lqw);
        log.info("num：" + num);
        if (num <= 0){
            throw new ServiceException(ResultCode.SLIDESHOW_NOT_EXISTED);
        }
        slideshowDao.deleteBatchIds(ids);
    }

    @Override
    public void saveOrUpdate(Slideshow slideshow) {
        Integer id = slideshow.getId();
        //1.判断轮播图url是否唯一
        String url = slideshow.getUrl();
        //url唯一标识
        boolean flag = false;
        if (url != null){

            //id不是空，是修改，断是否是原来的轮播图url
            if (id != null){
                Slideshow oldSlideshow = slideshowDao.selectById(id);
                //如果旧轮播图不为空 且旧轮播图的url和新轮播图的url相同
                if(oldSlideshow != null && url.equals(oldSlideshow.getUrl())) {
                    //url唯一
                    flag = true;
                }
            }
            //如果经过上面流程，url还不是唯一的
            if (flag == false){
                LambdaQueryWrapper<Slideshow> qw =
                        new LambdaQueryWrapper<>();
                qw.eq(Slideshow::getUrl,url);
                //根据url查找Slideshow对象
                Slideshow s = slideshowDao.selectOne(qw);
                //如果该对象存在，抛出异常：轮播图已存在
                if (s != null){
                    throw new ServiceException(ResultCode.SLIDESHOW_URL_EXISTED);
                }
                //如果该对象不存在，就设置轮播图更新时间为当前时间
                slideshow.setUploadTime(LocalDateTime.now());
            }
        }
        if (id == null){
            //id == null，新增操作
            //如果没有传入状态值，默认为启用
            if (slideshow.getStatus() == null){
                slideshow.setStatus("启用");
            }
            //将slideshow添加进数据库
            slideshowDao.insert(slideshow);
        }else {
            //id != null，修改操作
            //判断轮播图是否有效
            Slideshow s = slideshowDao.selectById(id);
            //根据传入的id查找是否有对应的轮播图，如果没有，表示要修改的轮播图不存在，抛出轮播图不存在异常
            if (s == null){
                throw new ServiceException(ResultCode.SLIDESHOW_NOT_EXISTED);
            }
            //如果有效，执行修改操作
            slideshowDao.updateById(slideshow);
        }
    }
}
